home *** CD-ROM | disk | FTP | other *** search
/ Kellogg's Amérique / Kellogg's Amérique / amazonie_en_danger.swf / scripts / fl / controls / LabelButton.as < prev    next >
Text File  |  2020-08-04  |  10KB  |  337 lines

  1. package fl.controls
  2. {
  3.    import fl.core.InvalidationType;
  4.    import fl.core.UIComponent;
  5.    import fl.events.ComponentEvent;
  6.    import fl.managers.IFocusManagerComponent;
  7.    import flash.display.DisplayObject;
  8.    import flash.events.Event;
  9.    import flash.events.KeyboardEvent;
  10.    import flash.events.MouseEvent;
  11.    import flash.text.TextField;
  12.    import flash.text.TextFieldType;
  13.    import flash.text.TextFormat;
  14.    import flash.ui.Keyboard;
  15.    
  16.    public class LabelButton extends BaseButton implements IFocusManagerComponent
  17.    {
  18.       
  19.       private static var defaultStyles:Object = {
  20.          "icon":null,
  21.          "upIcon":null,
  22.          "downIcon":null,
  23.          "overIcon":null,
  24.          "disabledIcon":null,
  25.          "selectedDisabledIcon":null,
  26.          "selectedUpIcon":null,
  27.          "selectedDownIcon":null,
  28.          "selectedOverIcon":null,
  29.          "textFormat":null,
  30.          "disabledTextFormat":null,
  31.          "textPadding":5,
  32.          "embedFonts":false
  33.       };
  34.       
  35.       public static var createAccessibilityImplementation:Function;
  36.        
  37.       
  38.       protected var _labelPlacement:String = "right";
  39.       
  40.       protected var _toggle:Boolean = false;
  41.       
  42.       protected var icon:DisplayObject;
  43.       
  44.       protected var oldMouseState:String;
  45.       
  46.       protected var mode:String = "center";
  47.       
  48.       public var textField:TextField;
  49.       
  50.       protected var _label:String = "Label";
  51.       
  52.       public function LabelButton()
  53.       {
  54.          _labelPlacement = ButtonLabelPlacement.RIGHT;
  55.          _toggle = false;
  56.          _label = "Label";
  57.          mode = "center";
  58.          super();
  59.       }
  60.       
  61.       public static function getStyleDefinition() : Object
  62.       {
  63.          return mergeStyles(defaultStyles,BaseButton.getStyleDefinition());
  64.       }
  65.       
  66.       protected function toggleSelected(param1:MouseEvent) : void
  67.       {
  68.          selected = !selected;
  69.          dispatchEvent(new Event(Event.CHANGE,true));
  70.       }
  71.       
  72.       public function get labelPlacement() : String
  73.       {
  74.          return _labelPlacement;
  75.       }
  76.       
  77.       override protected function keyDownHandler(param1:KeyboardEvent) : void
  78.       {
  79.          if(!enabled)
  80.          {
  81.             return;
  82.          }
  83.          if(param1.keyCode == Keyboard.SPACE)
  84.          {
  85.             if(oldMouseState == null)
  86.             {
  87.                oldMouseState = mouseState;
  88.             }
  89.             setMouseState("down");
  90.             startPress();
  91.          }
  92.       }
  93.       
  94.       protected function setEmbedFont() : *
  95.       {
  96.          var _loc1_:Object = null;
  97.          _loc1_ = getStyleValue("embedFonts");
  98.          if(_loc1_ != null)
  99.          {
  100.             textField.embedFonts = _loc1_;
  101.          }
  102.       }
  103.       
  104.       override protected function keyUpHandler(param1:KeyboardEvent) : void
  105.       {
  106.          if(!enabled)
  107.          {
  108.             return;
  109.          }
  110.          if(param1.keyCode == Keyboard.SPACE)
  111.          {
  112.             setMouseState(oldMouseState);
  113.             oldMouseState = null;
  114.             endPress();
  115.             dispatchEvent(new MouseEvent(MouseEvent.CLICK));
  116.          }
  117.       }
  118.       
  119.       override public function get selected() : Boolean
  120.       {
  121.          return !!_toggle ? Boolean(_selected) : false;
  122.       }
  123.       
  124.       public function set labelPlacement(param1:String) : void
  125.       {
  126.          _labelPlacement = param1;
  127.          invalidate(InvalidationType.SIZE);
  128.       }
  129.       
  130.       public function set toggle(param1:Boolean) : void
  131.       {
  132.          if(!param1 && super.selected)
  133.          {
  134.             selected = false;
  135.          }
  136.          _toggle = param1;
  137.          if(_toggle)
  138.          {
  139.             addEventListener(MouseEvent.CLICK,toggleSelected,false,0,true);
  140.          }
  141.          else
  142.          {
  143.             removeEventListener(MouseEvent.CLICK,toggleSelected);
  144.          }
  145.          invalidate(InvalidationType.STATE);
  146.       }
  147.       
  148.       public function get label() : String
  149.       {
  150.          return _label;
  151.       }
  152.       
  153.       override public function set selected(param1:Boolean) : void
  154.       {
  155.          _selected = param1;
  156.          if(_toggle)
  157.          {
  158.             invalidate(InvalidationType.STATE);
  159.          }
  160.       }
  161.       
  162.       override protected function draw() : void
  163.       {
  164.          if(textField.text != _label)
  165.          {
  166.             label = _label;
  167.          }
  168.          if(isInvalid(InvalidationType.STYLES,InvalidationType.STATE))
  169.          {
  170.             drawBackground();
  171.             drawIcon();
  172.             drawTextFormat();
  173.             invalidate(InvalidationType.SIZE,false);
  174.          }
  175.          if(isInvalid(InvalidationType.SIZE))
  176.          {
  177.             drawLayout();
  178.          }
  179.          if(isInvalid(InvalidationType.SIZE,InvalidationType.STYLES))
  180.          {
  181.             if(isFocused && focusManager.showFocusIndicator)
  182.             {
  183.                drawFocus(true);
  184.             }
  185.          }
  186.          validate();
  187.       }
  188.       
  189.       public function get toggle() : Boolean
  190.       {
  191.          return _toggle;
  192.       }
  193.       
  194.       override protected function configUI() : void
  195.       {
  196.          super.configUI();
  197.          textField = new TextField();
  198.          textField.type = TextFieldType.DYNAMIC;
  199.          textField.selectable = false;
  200.          addChild(textField);
  201.       }
  202.       
  203.       override protected function drawLayout() : void
  204.       {
  205.          var _loc1_:Number = NaN;
  206.          var _loc2_:String = null;
  207.          var _loc3_:Number = NaN;
  208.          var _loc4_:Number = NaN;
  209.          var _loc5_:Number = NaN;
  210.          var _loc6_:Number = NaN;
  211.          var _loc7_:Number = NaN;
  212.          var _loc8_:Number = NaN;
  213.          _loc1_ = Number(getStyleValue("textPadding"));
  214.          _loc2_ = icon == null && mode == "center" ? ButtonLabelPlacement.TOP : _labelPlacement;
  215.          textField.height = textField.textHeight + 4;
  216.          _loc3_ = textField.textWidth + 4;
  217.          _loc4_ = textField.textHeight + 4;
  218.          _loc5_ = icon == null ? Number(0) : Number(icon.width + _loc1_);
  219.          _loc6_ = icon == null ? Number(0) : Number(icon.height + _loc1_);
  220.          textField.visible = label.length > 0;
  221.          if(icon != null)
  222.          {
  223.             icon.x = Math.round((width - icon.width) / 2);
  224.             icon.y = Math.round((height - icon.height) / 2);
  225.          }
  226.          if(textField.visible == false)
  227.          {
  228.             textField.width = 0;
  229.             textField.height = 0;
  230.          }
  231.          else if(_loc2_ == ButtonLabelPlacement.BOTTOM || _loc2_ == ButtonLabelPlacement.TOP)
  232.          {
  233.             _loc7_ = Math.max(0,Math.min(_loc3_,width - 2 * _loc1_));
  234.             if(height - 2 > _loc4_)
  235.             {
  236.                _loc8_ = _loc4_;
  237.             }
  238.             else
  239.             {
  240.                _loc8_ = height - 2;
  241.             }
  242.             textField.width = _loc3_ = _loc7_;
  243.             textField.height = _loc4_ = _loc8_;
  244.             textField.x = Math.round((width - _loc3_) / 2);
  245.             textField.y = Math.round((height - textField.height - _loc6_) / 2 + (_loc2_ == ButtonLabelPlacement.BOTTOM ? _loc6_ : 0));
  246.             if(icon != null)
  247.             {
  248.                icon.y = Math.round(_loc2_ == ButtonLabelPlacement.BOTTOM ? Number(textField.y - _loc6_) : Number(textField.y + textField.height + _loc1_));
  249.             }
  250.          }
  251.          else
  252.          {
  253.             _loc7_ = Math.max(0,Math.min(_loc3_,width - _loc5_ - 2 * _loc1_));
  254.             textField.x = Math.round((width - (textField.width = Number(_loc7_)) - _loc5_) / 2 + (_loc2_ != ButtonLabelPlacement.LEFT ? _loc5_ : 0));
  255.             textField.y = Math.round((height - textField.height) / 2);
  256.             if(icon != null)
  257.             {
  258.                icon.x = Math.round(_loc2_ != ButtonLabelPlacement.LEFT ? Number(textField.x - _loc5_) : Number(textField.x + _loc3_ + _loc1_));
  259.             }
  260.          }
  261.          super.drawLayout();
  262.       }
  263.       
  264.       override protected function initializeAccessibility() : void
  265.       {
  266.          if(LabelButton.createAccessibilityImplementation != null)
  267.          {
  268.             LabelButton.createAccessibilityImplementation(this);
  269.          }
  270.       }
  271.       
  272.       protected function drawIcon() : void
  273.       {
  274.          var _loc1_:DisplayObject = null;
  275.          var _loc2_:* = null;
  276.          var _loc3_:Object = null;
  277.          _loc1_ = icon;
  278.          _loc2_ = !!enabled ? mouseState : "disabled";
  279.          if(selected)
  280.          {
  281.             _loc2_ = "selected" + _loc2_.substr(0,1).toUpperCase() + _loc2_.substr(1);
  282.          }
  283.          _loc2_ += "Icon";
  284.          _loc3_ = getStyleValue(_loc2_);
  285.          if(_loc3_ == null)
  286.          {
  287.             _loc3_ = getStyleValue("icon");
  288.          }
  289.          if(_loc3_ != null)
  290.          {
  291.             icon = getDisplayObjectInstance(_loc3_);
  292.          }
  293.          if(icon != null)
  294.          {
  295.             addChildAt(icon,1);
  296.          }
  297.          if(_loc1_ != null && _loc1_ != icon)
  298.          {
  299.             removeChild(_loc1_);
  300.          }
  301.       }
  302.       
  303.       public function set label(param1:String) : void
  304.       {
  305.          _label = param1;
  306.          if(textField.text != _label)
  307.          {
  308.             textField.text = _label;
  309.             dispatchEvent(new ComponentEvent(ComponentEvent.LABEL_CHANGE));
  310.          }
  311.          invalidate(InvalidationType.SIZE);
  312.          invalidate(InvalidationType.STYLES);
  313.       }
  314.       
  315.       protected function drawTextFormat() : void
  316.       {
  317.          var _loc1_:Object = null;
  318.          var _loc2_:TextFormat = null;
  319.          var _loc3_:TextFormat = null;
  320.          _loc1_ = UIComponent.getStyleDefinition();
  321.          _loc2_ = !!enabled ? _loc1_.defaultTextFormat as TextFormat : _loc1_.defaultDisabledTextFormat as TextFormat;
  322.          textField.setTextFormat(_loc2_);
  323.          _loc3_ = getStyleValue(!!enabled ? "textFormat" : "disabledTextFormat") as TextFormat;
  324.          if(_loc3_ != null)
  325.          {
  326.             textField.setTextFormat(_loc3_);
  327.          }
  328.          else
  329.          {
  330.             _loc3_ = _loc2_;
  331.          }
  332.          textField.defaultTextFormat = _loc3_;
  333.          setEmbedFont();
  334.       }
  335.    }
  336. }
  337.